This page contains JavaScript code that you can run in Internet Explorer's Console window (F12 Developer Tools) to perform helpful operations within the Dynamics CRM 2013/2015 Web UI.

List all attribute names and values for the entity that's in the current form

// INSTRUCTIONS:
// 1) Open an entity form for the data you want to retrieve.
// 2) Replace the "entity" variable below with the schema name (mixed-case) for the entity type you have open.
// 3) Run this script in the IE Developer Tools (F12) script window.
// The script opens a new window with the entity record's values listed, sorted by attribute schema name.
 
// REQUIREMENTS:
// - The JSON2 JavaScript library is required on the form. You can obtain it free and add the minified version to the form as a web resource.
 
// SET THIS TO THE ENTITY *SCHEMA* NAME...
var entity = "Account";
 
var dict = {};
var numAttributes = 0;
var numAttributesStored = 0;
 
var w = frames[1];
if (frames[0].Xrm.Page.entity) {
    w = frames[0];
}
 
// Retrieve all fields for the entity record
function StartDataRetrieval() {
    var serverUrl = w.Xrm.Page.context.getServerUrl();
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var recordId = w.Xrm.Page.data.entity.getId().substr(1,36);
    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", ODataPath + "/" + entity + "Set(guid'" + recordId + "')", true);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        dataRetrievalCallBack(this);
    };
    retrieveReq.send();
}
 
// Called from StartDataRetrieval. Loops through each attribute and retrieves the value.
function dataRetrievalCallBack(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var oRetrievedEntity = JSON.parse(retrieveReq.responseText).d;
            alert("OData query executed. Results will be displayed in a separate window after retrieving all entity values. Click OK to continue.");
            // Count the number of attributes retrieved
            for (var key in oRetrievedEntity) {
                numAttributes++;
            }
 
            for (var key in oRetrievedEntity) {
                QueryForAttributeValue(key);
            }
        }
    }
}
 
function QueryForAttributeValue(attributeSchemaName) {
    var serverUrl = w.Xrm.Page.context.getServerUrl();
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var recordId = w.Xrm.Page.data.entity.getId().substr(1,36);
    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", ODataPath + "/" + entity + "Set(guid'" + recordId + "')/" + attributeSchemaName, true);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        try {
            writeAttributeValueToWindow(this, attributeSchemaName);
        }
        catch (e) {
            // alert("Error displaying value for " + attributeSchemaName);
        }
    };
    retrieveReq.send();
}
 
function writeAttributeValueToWindow(retrieveReq, attributeSchemaName) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var oRetrievedEntity = JSON.parse(retrieveReq.responseText).d;
            var value = oRetrievedEntity[attributeSchemaName];
 
            if (value) {
                if (value.Name) {
                    // Lookup
                    dict[attributeSchemaName] = value.Name + "  (Id=" + value.Id + "; Type=" + value.LogicalName + ")";
                }
                else if (value.Value != null) {
                    // OptionSet (picklist), statecode, statuscode
                    dict[attributeSchemaName] = value.Value;
                }
                else if (typeof(value) == "object") {
                    dict[attributeSchemaName] = " ";
                }
                else if (typeof(value) == "number") {
                    dict[attributeSchemaName] = value;
                }
                else if (typeof(value) == "boolean") {
                    dict[attributeSchemaName] = value;
                }
                else {
                    if (value.indexOf("/Date(") == 0)
                        {
                            var dateValue = new Date(parseInt(value.replace('/Date(', '')));
                            dict[attributeSchemaName] = dateValue;
                        }
                        else
                        {
                            dict[attributeSchemaName] = value;
                        }
                    }
            }
            else {
                dict[attributeSchemaName] = " ";
            }
 
            numAttributesStored++;
 
            if (numAttributesStored >= numAttributes) {
                DisplayResults();
            }
        }
        else {
            numAttributesStored++;
            if (numAttributesStored >= numAttributes) {
                DisplayResults();
            }
        }
    }
}
 
// Output results to new HTML document, sorted by attribute schema name
function DisplayResults() {
    var resultsHtml = "";
    var keys = [], k, i, len;
 
    for (k in dict) {
        if (dict.hasOwnProperty(k)) {
            keys.push(k);
        }
    }
 
    keys.sort();
    len = keys.length;
 
    for (i = 0; i < len; i++) {
        k = keys[i];
        resultsHtml += "<tr><td>" + k + "</td><td>" + dict[k] + "</td></tr>";
    }
 
    var w = window.open();
    var d = w.document;
    d.open();
    d.write("<html><head><style>td {font-family: verdana;font-size: 11px;border: 1px solid LightGray;}</style></head><body><p><table>");
    d.write(resultsHtml + "</table></p></body></html>");
    d.close();
}
 
StartDataRetrieval();
 


.